package com.choudou5.tools.builder;

import cn.hutool.core.util.StrUtil;

import java.math.BigDecimal;
import java.util.HashMap;

/**
 * HashMap扩展
 */
public class MapBuilder<K,V> extends HashMap<K,V> {

    //静态创建
    public static MapBuilder cre(){
        return new MapBuilder();
    }

    public static MapBuilder cre(String key, Object val){
        return new MapBuilder().p(key, val);
    }

    public MapBuilder() {
        super();
    }

    public MapBuilder p(K key, V val) {
        this.put(key, val);
        return this;
    }

    public String getStr(String key) {
        Object obj = this.get(key);
        return obj==null?null:obj.toString();
    }

    public String getStr(String key, String def) {
        Object obj = this.get(key);
        if(obj == null)
            return def;
        String objStr = obj.toString();
        return (objStr.length()==0 || objStr.equals("null"))?def:objStr;
    }

    //格式化掉 小数
    private String formatDigitVal(String str){
        if(str.indexOf(".") == -1)
            return str;
        return StrUtil.subBefore(str, ".", false);
    }

    /**
     * 获得 int值
     * @param key
     * @return
     */
    public Integer getInt(String key) {
        Object obj = this.get(key);
        if(obj==null || obj.toString().length() == 0)
            return null;
        return Integer.valueOf(formatDigitVal(obj.toString()));
    }


    /**
     * 获得 int值
     * @param key
     * @param def 默认值
     * @return
     */
    public Integer getInt(String key, int def) {
        Object obj = this.get(key);
        if(obj==null || obj.toString().length() == 0)
            return def;
        return Integer.valueOf(formatDigitVal(obj.toString()));
    }


    /**
     * 获得 long值
     * @param key
     * @return
     */
    public Long getLong(String key) {
        Object obj = this.get(key);
        if(obj==null || obj.toString().length() == 0)
            return null;
        return Long.valueOf(formatDigitVal(obj.toString()));
    }

    /**
     * 获得 long值
     * @param key
     * @param def 默认值
     * @return
     */
    public Long getLong(String key, long def) {
        Object obj = this.get(key);
        if(obj==null || obj.toString().length() == 0)
            return def;
        return Long.valueOf(formatDigitVal(obj.toString()));
    }

    /**
     * 获得 float值
     * @param key
     * @return
     */
    public Float getFloat(String key) {
        Object obj = this.get(key);
        if(obj==null || obj.toString().length() == 0)
            return null;
        return Float.valueOf(obj.toString());
    }

    /**
     * 获得 float值
     * @param key
     * @param def 默认值
     * @return
     */
    public Float getFloat(String key, float def) {
        Object obj = this.get(key);
        if(obj==null || obj.toString().length() == 0)
            return def;
        return Float.valueOf(obj.toString());
    }

    /**
     * 获得 BigDecimal值
     * @param key
     * @return
     */
    public BigDecimal getBigDecimal(String key) {
        Object obj = this.get(key);
        if(obj==null || obj.toString().length() == 0)
            return null;
        if(obj instanceof BigDecimal)
            return (BigDecimal)obj;
        return new BigDecimal(obj.toString());
    }

    public Object poll(String key) {
        Object obj = this.get(key);
        if(obj !=null)
            this.remove(key);
        return obj;
    }

    public MapBuilder pollToNewMap(String ... keys) {
        MapBuilder newMap = MapBuilder.cre();
        for (String key : keys) {
            Object obj = poll(key);
            if(obj !=null)
                newMap.put(key, obj);
        }
        return newMap;
    }

}
